package handler

import (
	"context"
	"log"

	"category/common"
	"category/domain/model"
	"category/domain/service"
	pb "category/proto"
)

type Category struct {
	CategoryDataService service.ICategoryDataService
}

// 创建分类
func (c *Category) CreateCategory(ctx context.Context, in *pb.CategoryRequest, out *pb.CreateCategoryResponse) error {
	category := &model.Category{}
	// 赋值
	err := common.SwapTo(in, category)
	if err != nil {
		return err
	}
	// 调用service里的对应方法
	categoryId, err := c.CategoryDataService.AddCategory(category)
	if err != nil {
		return err
	}
	out.Message = "分类添加成功"
	out.CategoryId = categoryId
	return nil
}

// 更新分类
func (c *Category) UpdateCategory(ctx context.Context, in *pb.CategoryRequest, out *pb.UpdateCategoryResponse) error {
	category := &model.Category{}
	// 赋值
	err := common.SwapTo(in, category)
	if err != nil {
		return err
	}
	// 调用service里的对应方法
	err = c.CategoryDataService.UpdateCategory(category)
	if err != nil {
		return err
	}
	out.Message = "更新分类成功"
	return nil

}

// 分类删除
func (c *Category) DeleteCategory(ctx context.Context, in *pb.DeleteCategoryRequest, out *pb.DeleteCategoryResponse) error {
	err := c.CategoryDataService.DeleteCategory(in.CategoryId)
	if err != nil {
		return err
	}
	out.Message = "分类删除成功"
	return nil
}

// 根据名称查询分类
func (c *Category) FindCategoryByName(ctx context.Context, in *pb.FindByNameRequest, out *pb.CategoryResponse) error {
	category, err := c.CategoryDataService.FindCategoryByName(in.CategoryName)
	if err != nil {
		return err
	}
	return common.SwapTo(category, out)
}

// 根据ID查询分类
func (c *Category) FindCategoryByID(ctx context.Context, in *pb.FindByIDRequest, out *pb.CategoryResponse) error {
	category, err := c.CategoryDataService.FindCategoryByID(in.CategoryId)
	if err != nil {
		return err
	}
	return common.SwapTo(category, out)
}

// 根据层别查询分类
func (c *Category) FindCategoryByLevel(ctx context.Context, in *pb.FindByLevelRequest, out *pb.FindAllResponse) error {
	categorySlice, err := c.CategoryDataService.FindCategoryByLevel(in.Level)
	if err != nil {
		return err
	}
	categoryToOut(categorySlice, out)
	return nil
}

// 根据父ID查询分类
func (c *Category) FindCategoryByParent(ctx context.Context, in *pb.FindByParentRequest, out *pb.FindAllResponse) error {
	categorySlice, err := c.CategoryDataService.FindCategoryByParent(in.ParentId)
	if err != nil {
		return err
	}
	categoryToOut(categorySlice, out)
	return nil
}

// 获取所有分类
func (c *Category) FindAllCategory(ctx context.Context, in *pb.FindAllRequest, out *pb.FindAllResponse) error {
	categorySlice, err := c.CategoryDataService.FindAllCategory()
	if err != nil {
		return err
	}
	categoryToOut(categorySlice, out)
	return nil
}

// []model.Category 转为 out
func categoryToOut(categorySlice []model.Category, out *pb.FindAllResponse) {
	for _, cate := range categorySlice {
		cateResponse := &pb.CategoryResponse{}
		err := common.SwapTo(cate, cateResponse)
		if err != nil {
			log.Println(err)
			break
		}
		out.Category = append(out.Category, cateResponse)
	}
}
